<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vcode extends CI_Controller {

	public function index()
	{
		$this->get_code(100, 24);
	}
	
	private function get_code($w, $h){
		$im = imagecreate($w, $h);
	
		//imagecolorallocate($im, 14, 114, 180); // background color
		$red = imagecolorallocate($im, 255, 0, 0);
		$white = imagecolorallocate($im, 255, 255, 255);
	
		$num1 = rand(1, 20);
		$num2 = rand(1, 20);
	
		$this->load->library('encrypt');
		$this->load->library('session');
		$this->session->set_flashdata('vcode_tmp', $this->encrypt->encode($num1 + $num2));
		//echo $this->encrypt->decode($this->session->flashdata('vcode_tmp'));
	
		$gray = imagecolorallocate($im, 118, 151, 199);
		$black = imagecolorallocate($im, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
	
		//画背景
		imagefilledrectangle($im, 0, 0, 100, 24, $black);
		//在画布上随机生成大量点，起干扰作用;
		for ($i = 0; $i < 80; $i++) {
			imagesetpixel($im, rand(0, $w), rand(0, $h), $gray);
		}
	
		imagestring($im, 5, rand(5, 10), 4, $num1, $red);
		imagestring($im, 5, rand(25, 30), 3, "+", $red);
		imagestring($im, 5, rand(35, 45), 4, $num2, $red);
		imagestring($im, 5, rand(60, 65), 3, "=", $red);
		imagestring($im, 5, rand(75, 80), 2, "?", $white);
	
		header("Content-type: image/png");
		imagepng($im);
		imagedestroy($im);
	}
	
}


